<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>公告管理</el-breadcrumb-item>
      <el-breadcrumb-item>公告管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="4">
          <AddNews @addSuccess="getNewsList($event)"/>
        </el-col>
      </el-row>
      <el-table :data="newsList" border stripe style="width: 100%" height="640">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="title" label="公告名称"></el-table-column>
        <el-table-column label="状态">
          <template v-slot="scope">
            <el-switch
                active-color="#13ce66"
                inactive-color="red"
                v-model="scope.row.status"
                :active-value="activeValue"
                :inactive-value="inactiveValue"
                @change="newsStateChanged(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="发布时间">
          <template v-slot="scope">
            {{ scope.row.createTime | formatDate }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <!-- 编辑公告 -->
            <EditNews style="float: left" :news-id="scope.row.id" @editSuccess="getNewsList($event)"/>
            <!-- 删除用公告 -->
            <DelNews style="float: left;margin-left: 10px" :news-id="scope.row.id" @delSuccess="getNewsList"/>
          </template>
        </el-table-column>
      </el-table>
      <Pageinfo :total="total" @updatePageInfo="updatePageInfo($event)"/>
    </el-card>

  </div>
</template>

<script>
import Pageinfo from "@/components/pageInfo/pageinfo";
import AddNews from "@/components/news/addNews/AddNews";
import EditNews from "@/components/news/editNews/EditNews";
import DelNews from "@/components/news/delNews/DelNews";
export default {
  components: {DelNews, EditNews, AddNews, Pageinfo},
  data() {
    return {
      activeValue: 1,
      inactiveValue: 0,
      newsList: [],
      total: 0,
      pageInfo: {}
    };
  },
  methods: {
    async getNewsList() {
      const { data } = await this.axios.post("/admin/news/getAll", this.pageInfo);
      this.newsList = data.data.list;
      this.total = data.data.total;
    },
    async newsStateChanged(news) {
      const { data } = await this.axios.get("/admin/news/updateStatus", {params:{id: news.id,status: news.status}})
      if (data.code == 1) {
        this.$notify.success({
          title: "更新成功！",
          message: "更新新闻@"+news.title+"状态成功！"
        })
      }else{
        this.$notify.error({
          title: "更新失败！",
          message: "更新新闻@"+news.title+"状态失败！"
        })
      }
    },
    updatePageInfo(pageInfo) {
      this.pageInfo = pageInfo
      this.getNewsList()
    }
  }
};
</script>

<style lang="less" scoped></style>
